angular.module('Controller')
.controller('QgyBuildingManageEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    type=items.type
    $scope.type=items.type
    $scope.data=items.data
    $scope.info=items.info
    $scope.modalTitle=if type=='new' then '新建公寓楼栋' else '编辑公寓楼栋'
    $scope.modalEnableList=commonConstant.enabledList()

    _.defer(()->
        $('#photo-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif','jpeg'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                result = data.response?.message
                saveUrl =result['saveUrl']
                if(saveUrl.indexOf("http")==0)
                    $scope.data.layoutImage = saveUrl
                else
                    $scope.data.layoutImage=urls.backend+'/public/'+saveUrl

                $scope.showPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
        $('#thumb-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif','jpeg'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                result = data.response?.message
                saveUrl =result['saveUrl']
                if(saveUrl.indexOf("http")==0)
                    $scope.data.thumb = saveUrl
                else
                    $scope.data.thumb=urls.backend+'/public/'+saveUrl

                $scope.showThumbPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
    )
    loadApartment=()->
        requestData={
            page: 0,
            size: 999
        }
        $http.get('/apartment',params:requestData)
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=resp.message.content
                if $scope.apartmentList?.length>0
                    $scope.data.apartmentId=$scope.apartmentList[0].id
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.floor={
        settingWay:'slider'
        sliderData:[1,2,3,4,5,6,7,8,9,10]
        textData:"1,2,3,4,5,6,7,8,9,10"
        data:[1,2,3,4,5,6,7,8,9,10]
    }
    $scope.slider = {
        minValue: 1,
        maxValue: 10,
        options: {
            floor: 1,
            ceil: 50,
            step: 1,
            minRange: 1,
            noSwitching: true,
            onEnd: ()->
                $scope.floor.sliderData=[$scope.slider.minValue..$scope.slider.maxValue]
                $scope.floor.textData=[$scope.slider.minValue..$scope.slider.maxValue].toString()
                $scope.floor.data=$scope.floor.sliderData
                console.log($scope.floor.textData)
        }
    };
    $scope.reflectSlider=()->
        if $scope.floor.textData.length==0
            $scope.floor.textData=$scope.floor.data[0]
            return
        newData=$scope.floor.textData.split(',')
        if _.every(newData,(theItem)->parseInt(theItem) in [1..99])==false
            return
        list=[]
        _.each newData,(item)->
            if parseInt(item) in [1..99] and _.some(list,(listItem)->listItem==parseInt(item))==false
                list.push parseInt(item)
        list=_.sortBy(list,(item)->return --item)
#        console.log('新楼层数据：',list)
        if list.length!=$scope.floor.data.length
            $scope.floor.data=list
            $scope.slider.minValue=$scope.floor.data[0]
            $scope.slider.maxValue=$scope.floor.data[$scope.floor.data.length-1]
            $scope.floor.textData=$scope.floor.data
    _.extend $scope,
        newData:()->
            floorData=[]
            _.each $scope.floor.data,(item)->
                floorData.push {id:parseInt(item),name:'第'+item+'层',enabled:true,thumb:''}
            reqData={
                "apartmentId" : $scope.info.apartmentId || $scope.data.apartmentId
                "name":  $scope.data.name
                "floorDesc":floorData, #楼栋楼层
                "tel" : $scope.data.tel
                "address":$scope.data.address, #地址
                "houseNum":$scope.data.houseNum, #房间数，必传
                "adminUid":1 #管理员id，必传，暂无管理员数据，先写死
                "thumb":$scope.data.thumb
                "brief" : $scope.data.brief
                "description" : $scope.data.description
                "enabled" : $scope.data.enabled
            }
            $http.post('/building',reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
            reqData={
                "name":  $scope.data.name
                "tel" : $scope.data.tel
                "address":$scope.data.address, #地址
                "houseNum":$scope.data.houseNum, #房间数，必传
                "thumb":$scope.data.thumb
                "brief" : $scope.data.brief
                "description" : $scope.data.description
                "enabled" : $scope.data.enabled
            }
            $http.put("/building/#{$scope.data.id}",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)